<%@ page language="java" contentType="text/html; charset=gbk"
	pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.sql.*"%>

<%
	request.setCharacterEncoding("gbk");
	String title = request.getParameter("title");
	String cont = request.getParameter("cont");

	cont = cont.replaceAll("\n", "<br>");

	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost/bbs?user=root&password=123456";
	Connection conn = DriverManager.getConnection(url);

	Statement stat = conn.createStatement();
	//stat.executeUpdate(

	conn.setAutoCommit(false);
	String sql = "insert into article values (null,0,?,?,?,now(),0)";
	PreparedStatement pstat = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);

	pstat.setInt(1, -1);
	pstat.setString(2, title);
	pstat.setString(3, cont);
	pstat.executeUpdate();

	ResultSet reset = pstat.getGeneratedKeys();
	reset.next();
	int key = reset.getInt(1);
	reset.close();

	String resql = "update article set rootid=" + key + " where id="+ key;

	stat.executeUpdate(resql);

	conn.commit();
	conn.setAutoCommit(true);

	pstat.close();
	stat.close();
	conn.close();

	response.sendRedirect("ShowArticleFlat.jsp");
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
	<p></p>
</body>
</html>